using HTCollections.Custom;
using UnityEngine;

namespace HTCollections.Examples.CustomCollections
{
    public class CustomCollectionsExample : MonoBehaviour
    {
        FastList<int> mFastLst;
        FastHashMap<string, int> mFastHashMap;

        void Start()
        {
            mFastLst = new FastList<int>();
            mFastHashMap = new FastHashMap<string, int>(6);
        }

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Q)) //FastList
            {
                //添加元素
                mFastLst.Add(1);
                mFastLst.Add(3);
                mFastLst.Add(5);
                mFastLst.Add(7);
                Debug.Log($"FastList capacity : {mFastLst.Capacity}, count : {mFastLst.Length}");
                foreach (var item in mFastLst)
                {
                    Debug.Log($"FastList : {item}");
                }

                //列表扩容 (Expend)
                mFastLst.Add(9);
                Debug.Log($"FastList capacity : {mFastLst.Capacity}, count : {mFastLst.Length}");
                foreach (var item in mFastLst)
                {
                    Debug.Log($"FastList : {item}");
                }

                //删除元素
                mFastLst.Remove(6);
                Debug.Log($"FastList after remove 6 capacity : {mFastLst.Capacity}, count : {mFastLst.Length}");
                mFastLst.Remove(5);
                Debug.Log($"FastList after remove 5 capacity : {mFastLst.Capacity}, count : {mFastLst.Length}");
                foreach (var item in mFastLst)
                {
                    Debug.Log($"FastList : {item}");
                }

                //清空列表
                mFastLst.Clear();
                Debug.Log($"FastList after clear capacity : {mFastLst.Capacity}, count : {mFastLst.Length}");
                foreach (var item in mFastLst)
                {
                    Debug.Log($"FastList : {item}");
                }
            }

            if (Input.GetKeyDown(KeyCode.W)) //FastHashMap
            {
                //添加键值对
                mFastHashMap.Add("A1", 10);
                mFastHashMap.Add("B1", 20);
                mFastHashMap.Add("C1", 30);
                mFastHashMap.Add("D1", 40);
                Debug.Log($"FastHashMap capacity : {mFastHashMap.Capacity}, count : {mFastHashMap.Length}");
                foreach (var item in mFastHashMap)
                {
                    Debug.Log($"FastHashMap : {item.Key}, {item.Value}");
                }

                //哈希表扩容 (Expend)
                mFastHashMap.Add("E1", 50);
                Debug.Log($"FastHashMap capacity : {mFastHashMap.Capacity}, count : {mFastHashMap.Length}");
                foreach (var item in mFastHashMap)
                {
                    Debug.Log($"FastHashMap : {item.Key}, {item.Value}");
                }

                //获取
                if (mFastHashMap.TryGetValue("B2", out var valueB2))
                    Debug.Log($"FastHashMap TryGetValue B2 : {valueB2}");
                if (mFastHashMap.TryGetValue("B1", out var valueB1))
                    Debug.Log($"FastHashMap TryGetValue B1 : {valueB1}");

                //查询
                Debug.Log($"FastHashMap {(mFastHashMap.ContainsKey("D1") ? "包含" : "不包含")} D1");
                Debug.Log($"FastHashMap {(mFastHashMap.ContainsKey("D2") ? "包含" : "不包含")} D2");

                //删除键值对
                mFastHashMap.Remove("C2");
                Debug.Log(
                    $"FastHashMap after remove C2 capacity : {mFastHashMap.Capacity}, count : {mFastHashMap.Length}");
                mFastHashMap.Remove("C1");
                Debug.Log(
                    $"FastHashMap after remove C1 capacity : {mFastHashMap.Capacity}, count : {mFastHashMap.Length}");
                foreach (var item in mFastHashMap)
                {
                    Debug.Log($"FastHashMap : {item.Key}, {item.Value}");
                }

                mFastHashMap.Remove("D1");
                mFastHashMap.Remove("A1");
                Debug.Log(
                    $"FastHashMap after remove D1 A1 capacity : {mFastHashMap.Capacity}, count : {mFastHashMap.Length}");
                foreach (var item in mFastHashMap)
                {
                    Debug.Log($"FastHashMap : {item.Key}, {item.Value}");
                }


                FastHashMap<string, int> addMap = new FastHashMap<string, int>(4);
                addMap.Add("A2", 12);
                addMap.Add("B2", 22);
                addMap.Add("C2", 32);
                addMap.Add("D2", 42);
                addMap.Add("A3", 13);
                addMap.Add("B3", 23);
                addMap.Add("C3", 33);
                addMap.Add("D3", 43);
                addMap.Add("A4", 14);
                // addMap.Add("B4", 24);
                // addMap.Add("C4", 34);
                // addMap.Add("D4", 44);
                // addMap.Add("A5", 15);
                // addMap.Add("B5", 25);
                // addMap.Add("C5", 35);
                // addMap.Add("D5", 45);
                // addMap.Add("A6", 16);
                // addMap.Add("B6", 26);
                // addMap.Add("C6", 36);
                // addMap.Add("D6", 46);
                // addMap.Add("A7", 17);
                // addMap.Add("B7", 27);
                // addMap.Add("C7", 37);
                // addMap.Add("D7", 47);
                mFastHashMap.AddRange(addMap);
                Debug.Log(
                    $"FastHashMap after add A2 B2 C2 D2 capacity : {mFastHashMap.Capacity}, count : {mFastHashMap.Length}");
                foreach (var item in mFastHashMap)
                {
                    Debug.Log($"FastHashMap : {item.Key}, {item.Value}");
                }

                //清空哈希表
                mFastHashMap.Clear();
                Debug.Log($"FastHashMap after clear capacity : {mFastHashMap.Capacity}, count : {mFastHashMap.Length}");
                foreach (var item in mFastHashMap)
                {
                    Debug.Log($"FastHashMap : {item.Key}, {item.Value}");
                }
            }

            if (Input.GetKeyDown(KeyCode.E)) //优先级队列（小顶堆）
            {
                /*
                IntPriorityQueue priorityQueue = new IntPriorityQueue();
                priorityQueue.Enqueue(4);
                priorityQueue.Enqueue(1);
                priorityQueue.Enqueue(9);
                priorityQueue.Enqueue(7);
                priorityQueue.Enqueue(2);
                Debug.Log($"peek : {priorityQueue.Peek()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"peek : {priorityQueue.Peek()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                */

                PriorityQueue<int> priorityQueue = new PriorityQueue<int>();
                priorityQueue.Enqueue(101, 4);
                priorityQueue.Enqueue(201, 1);
                priorityQueue.Enqueue(301, 9);
                priorityQueue.Enqueue(401, 7);
                priorityQueue.Enqueue(501, 2);
                Debug.Log($"peek : {priorityQueue.Peek()}"); //201
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //201
                Debug.Log($"peek : {priorityQueue.Peek()}"); //501
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //501
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //101
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //401
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //301
            }

            if (Input.GetKeyDown(KeyCode.R)) //优先级队列（大顶堆）
            {
                /*
                IntPriorityQueue priorityQueue = new IntPriorityQueue(true);
                priorityQueue.Enqueue(4);
                priorityQueue.Enqueue(1);
                priorityQueue.Enqueue(9);
                priorityQueue.Enqueue(7);
                priorityQueue.Enqueue(2);
                Debug.Log($"peek : {priorityQueue.Peek()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"peek : {priorityQueue.Peek()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}");
                */

                PriorityQueue<int> priorityQueue = new PriorityQueue<int>(true);
                priorityQueue.Enqueue(101, 4);
                priorityQueue.Enqueue(201, 1);
                priorityQueue.Enqueue(301, 9);
                priorityQueue.Enqueue(401, 7);
                priorityQueue.Enqueue(501, 2);
                Debug.Log($"peek : {priorityQueue.Peek()}"); //301
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //301
                Debug.Log($"peek : {priorityQueue.Peek()}"); //401
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //401
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //101
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //501
                Debug.Log($"dequeue : {priorityQueue.Dequeue()}"); //201
            }
        }
    }
}